package graduation.controller;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import graduation.entity.Apply;
import graduation.entity.User;
import graduation.service.ApplyService;
import graduation.service.UserService;
import graduation.utils.ResultVOUtil;
import graduation.vo.ApplyDealVO;
import graduation.vo.ApplyVO;
import graduation.vo.ResultVO;

@Controller
@RequestMapping("/apply")
public class ApplyController {

	@Autowired
	private ApplyService applyServiceImpl;
	@Autowired
	private UserService userServiceImpl;
	/**
	 * 申请
	 * @param sender
	 * @param name
	 * @param receiver
	 * @param type
	 * @param daterange
	 * @param info
	 * @param outAddress
	 * @return
	 */
	@RequestMapping(value="addApply",method=RequestMethod.POST)//用来处理请求地址映射的注解
	@ResponseBody//@Responsebody 注解表示该方法的返回的结果直接写入 HTTP 响应正文（ResponseBody）中
	public ResultVO addApply(/*@RequestParam("sender") String sender,
			@RequestParam("name")  String name,
			@RequestParam("receiver")  String receiver,
			@RequestParam("type")  int type,
			@RequestParam("dateRange")  String dateRange,
			@RequestParam("info")  String info,
			@RequestParam("outAddress") String outAddress,
			@RequestParam("user") String userId*/
			@RequestBody ApplyVO applyVO
			){
		Apply apply = new Apply();
		apply.setSender(applyVO.getSender());
		apply.setName(applyVO.getName());
		apply.setReceiver(applyVO.getReceiver());
		apply.setType(applyVO.getType());
		apply.setDateRange(applyVO.getDateRange());
		apply.setInfo(applyVO.getInfo());
		apply.setOutAddress(applyVO.getOutAddress());
		apply.setUserId(applyVO.getUserId());
		//保存到数据库
		applyServiceImpl.addApply(apply);
		return ResultVOUtil.success("申请成功",apply);
	}
	/**
	 * 批阅
	 * @param id
	 * @param dealStatus
	 * @param dealResult
	 * @return
	 */
	@RequestMapping(value="deal",method=RequestMethod.POST)//用来处理请求地址映射的注解
	@ResponseBody//@Responsebody 注解表示该方法的返回的结果直接写入 HTTP 响应正文（ResponseBody）中
	public ResultVO deal(@RequestBody ApplyDealVO ApplyDealVO/*@RequestParam("dealId") Integer dealId,@RequestParam("dealStatus") Boolean dealStatus,@RequestParam("dealResult") Boolean dealResult*/){
		//通过id查询对应的申请
		Apply apply = applyServiceImpl.findById(ApplyDealVO.getDealId());
		
		apply.setDealStatus(ApplyDealVO.getDealStatus());
		apply.setDealresult(ApplyDealVO.getDealResult());
		
		//修改更新
		applyServiceImpl.update(apply);
		
		return ResultVOUtil.success("批阅完毕",apply);
		
	}
	
	/**
	 * 通过我的邮箱获取我的所有申请
	 * @param sender
	 * @return
	 */
	@RequestMapping(value="getMyApply",method=RequestMethod.GET)//用来处理请求地址映射的注解
	@ResponseBody//@Responsebody 注解表示该方法的返回的结果直接写入 HTTP 响应正文（ResponseBody）中
	public ResultVO getMyApply(@RequestParam("sender") String sender){
		//通过邮箱查询，返回list
		List list = applyServiceImpl.findBySender(sender);
		return ResultVOUtil.success("批阅完毕",list);
		
	}
	
	/**
	 * 通过接收人邮箱获取所有申请
	 * @param receiver
	 * @return
	 */
	@RequestMapping(value="getApplyByReceiver",method=RequestMethod.GET)//用来处理请求地址映射的注解
	@ResponseBody//@Responsebody 注解表示该方法的返回的结果直接写入 HTTP 响应正文（ResponseBody）中
	public ResultVO getApplyByReceiver(@RequestParam("receiver") String receiver){
		//通过接收人邮箱查询，返回list
		List list = applyServiceImpl.findByReceiver(receiver);
		
		return ResultVOUtil.success(list);
		
	}
	
	@RequestMapping(value="getMyApplyByStatus",method=RequestMethod.GET)//用来处理请求地址映射的注解
	@ResponseBody//@Responsebody 注解表示该方法的返回的结果直接写入 HTTP 响应正文（ResponseBody）中
	public ResultVO getMyApplyByStatus(@RequestParam("sender") String sender,@RequestParam("dealStatus") Boolean dealStatus){
		//通过接收人邮箱查询，返回list
		List list = applyServiceImpl.getMyApplyByStatus(sender,dealStatus);
		
		return ResultVOUtil.success(list);
		
	}
	/**
	 * 根据id删除
	 * 字段 ids,ids为数组，里面的传过来的id要为int类型
	 * @param map
	 * @return
	 */
	@RequestMapping(value="delApplyById",method=RequestMethod.POST)//用来处理请求地址映射的注解
	@ResponseBody//
	public ResultVO delApplyById(@RequestBody Map<String, Object> map){
		List applyList = (List) map.get("ids");
		for(int i = 0 ; i < applyList.size() ; i++) {
			applyServiceImpl.delApplyById((int)applyList.get(i));
			}
		return ResultVOUtil.success("删除成功");
	}
	
	
	/**
	 * 参数 ids，reades根据申请id修改申请reads字段
	 * @param map
	 * @return
	 */
	@RequestMapping(value="setApplyRead",method=RequestMethod.POST)//用来处理请求地址映射的注解
	@ResponseBody//
	public ResultVO setApplyRead(@RequestBody Map<String, Object> map){
		//获取id数组
		List ids = (List) map.get("ids");
		//获取reads
		int reades = (int) map.get("reades");
		//遍历id数组
		for(int i = 0; i < ids.size(); i++){

			Apply apply = applyServiceImpl.findById((int)ids.get(i));
			if(apply != null){
				if(reades == 0){
					apply.setReades(false);
				}else{
					apply.setReades(true);
				}
				applyServiceImpl.update(apply);
			}
		}
		
		return ResultVOUtil.success("修改成功");
	}
}
